﻿using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Web;
using System.Web.UI;

namespace MyCommon
{
    /// <summary>
    /// 获取指定程序集的文件版本，日志级别的信息 --郭旭东
    /// </summary>
    public class InfoAboutAssembly
    {
        /// <summary>
        /// 通过程序集填充信息
        /// </summary>
        /// <param name="assembly"></param>
        public void FillDataByAssembly(Assembly assembly)
        {
            var assemblyName = assembly.GetName();
            var assemblyPath = new Uri(assemblyName.CodeBase).LocalPath;
            AssemblyVersion = assemblyName.Version.ToString();
            FileVersion = FileVersionInfo.GetVersionInfo(assemblyPath).FileVersion;
            var log = log4net.LogManager.GetLogger(GetType());
            IsDebugLogEnable = log.IsDebugEnabled;
            IsInfoLogEnable = log.IsInfoEnabled;
            IsWarnLogEnable = log.IsWarnEnabled;
            IsErrorLogEnable = log.IsErrorEnabled;
            IsFatalLogEnable = log.IsFatalEnabled;
            PublishedTime = File.GetLastWriteTime(assemblyPath);
        }
        /// <summary>
        /// 致命日志是否可记录
        /// </summary>
        public bool IsFatalLogEnable { get; set; }
        /// <summary>
        /// 错误日志是否可记录
        /// </summary>
        public bool IsErrorLogEnable { get; set; }
        /// <summary>
        /// 警告日志是否可记录
        /// </summary>
        public bool IsWarnLogEnable { get; set; }
        /// <summary>
        /// 信息日志是否可记录
        /// </summary>
        public bool IsInfoLogEnable { get; set; }
        /// <summary>
        /// 调试日志是否可记录
        /// </summary>
        public bool IsDebugLogEnable { get; set; }
        /// <summary>
        /// 程序集版本
        /// </summary>
        public string AssemblyVersion { get; set; }
        /// <summary>
        /// 文件版本
        /// </summary>
        public string FileVersion { get; set; }
        /// <summary>
        /// 发布时间
        /// </summary>
        public DateTime PublishedTime { get; set; }


        internal void RenderHtml(HtmlTextWriter writer)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Ul);
            writer.RenderBeginTag(HtmlTextWriterTag.Li);
            writer.Write(string.Format("允许Debug日志输出：{0}", IsDebugLogEnable));
            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Li);
            writer.Write(string.Format("允许Info日志输出：{0}", IsInfoLogEnable));
            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Li);
            writer.Write(string.Format("允许War日志输出：{0}", IsWarnLogEnable));
            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Li);
            writer.Write(string.Format("允许Error日志输出：{0}", IsErrorLogEnable));
            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Li);
            writer.Write(string.Format("允许Fatal日志输出：{0}", IsFatalLogEnable));
            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Li);
            writer.Write(string.Format("程序集版本号：{0}", AssemblyVersion));
            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Li);
            writer.Write(string.Format("文件版本号：{0}", FileVersion));
            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Li);
            writer.Write(string.Format("发布时间：{0}", PublishedTime));
            writer.RenderEndTag();
            writer.RenderEndTag();
        }
    }
    /// <summary>
    /// 获取程序集信息的一般处理程序  --郭旭东
    /// </summary>
    public abstract class InfoAboutAssemblyHandler : IHttpHandler
    {
        /// <summary>
        /// 关联的程序集，将获取该程序集的相关信息，如版本号，发布时间等
        /// </summary>
        protected abstract Assembly Assembly { get; }
        /// <summary>
        /// 是否可重用
        /// </summary>
        public bool IsReusable
        {
            get { return true; }
        }
        /// <summary>
        /// 处理请求
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            var info = new InfoAboutAssembly();
            info.FillDataByAssembly(Assembly);
            context.Response.ContentType = "text/html";
            var writer = new HtmlTextWriter(context.Response.Output);
            info.RenderHtml(writer);
        }
    }
}
